// pages/grid4_chat/operation-area/index.js

const uploadImage = require('../../../utils/API.js').uploadImage

Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {

        // 底部操作面板是否展开
        operationPanelOpen: false,
        // 输入框是否有文字
        hasInputText: false,
        // 输入框的文字
        inputText: '',

        // 文字输入还是语音输入切换
        isKeyboardInputMode: true,

        // 正在录制
        isRecordingVoice: false
    },

    /**
     * 组件的方法列表
     */
    methods: {

        // 底部操作面板函数
        showOperationPanel() {
            this.setData({
                operationPanelOpen: true
            })
        },
        closeOperationPanel() {
            this.setData({
                operationPanelOpen: false
            })
        },
        toggleOperationPanel() {
            this.setData({
                operationPanelOpen: !this.data.operationPanelOpen
            })
        },

        // 检测输入框是否有文字
        onInputText(e) {
            this.setData({
                inputText: e.detail.value,
                hasInputText: e.detail.value.length > 0
            })
        },

        // 从输入框中发送文本消息
        sendTextMessage() {
            if (this.data.inputText.length === 0) return;
            this.triggerEvent('sendtextmessage', {
                message: this.data.inputText,
                success: () => {
                    console.log('send message ok')
                    this.setData({
                        inputText: ''
                    })
                },
                failed: e => {
                    console.log('send message failed', e)
                }
            })
        },

        // 从相册选取图片发送
        sendPictureFromGallery() {
            // 本地选取图片
            wx.chooseImage({
                sourceType: ['album'],
                success: res => {
                    const tempFilePaths = res.tempFilePaths
                    tempFilePaths.forEach(filepath => {

                        this.triggerEvent('sendpicturemessage', {
                            imagePath: filepath,
                            success: () => {
                                console.log('send picture ok')
                            },
                            failed: e => {
                                wx.showToast({
                                    title: '发送图片失败',
                                    icon: 'none'
                                })
                            }
                        })

                    })

                }
            })
        },

        // 从相机拍摄图片发送
        sendPictureFromCamera() {
            wx.chooseImage({
                sourceType: ['camera'],
                success: res => {
                    const tempFilePaths = res.tempFilePaths
                    tempFilePaths.forEach(filepath => {

                        this.triggerEvent('sendpicturemessage', {
                            imagePath: filepath,
                            success: () => {
                                console.log('send picture ok')
                            },
                            failed: e => {
                                wx.showToast({
                                    title: '发送图片失败',
                                    icon: 'none'
                                })
                            }
                        })

                    })

                }
            })
        },

        sendVideo() {
            wx.chooseVideo({
                sourceType: ['album', 'camera'],
                compressed: true,
                maxDuration: 60,
                camera: 'back',
                success: res => {
                    const filePath = res.tempFilePath
                    this.triggerEvent('sendvideomessage', {
                        videoPath: filePath,
                        success: () => {
                            console.log('send video ok')
                        },
                        failed: e => {
                            wx.showToast({
                                title: '发送视频失败',
                                icon: 'none'
                            })
                        }

                    })
                }
            })
        },

        toggleInputMode() {
            this.setData({
                isKeyboardInputMode: !this.data.isKeyboardInputMode
            })
        },
        bindRecordVoiceButtonTouchStart() {
            if (this.data.isRecordingVoice) {
                this.finishRecordingVoice()
            } else {
                this.startRecordingVoice()
            }
        },
        bindRecordVoiceButtonTouchEnd() {
            this.finishRecordingVoice()
        },
        // 开始录制
        startRecordingVoice() {
            this.setData({
                isRecordingVoice: true
            })



            const recorderManager = wx.getRecorderManager()

            recorderManager.onStart(() => {
                console.log('recorder start')
            })
            recorderManager.onStop((res) => {
                console.log('recorder stop', res)

                const filePath = res.tempFilePath

                this.triggerEvent('sendvoicemessage', {
                    voicePath: filePath,
                    success: () => {
                        console.log('send voice ok')
                    },
                    failed: e => {
                        wx.showToast({
                            title: '发送语音失败',
                            icon: 'none'
                        })
                        console.log(e)
                    }
                })
            })
            recorderManager.onFrameRecorded((res) => {
                const {
                    frameBuffer
                } = res
                console.log('frameBuffer.byteLength', frameBuffer.byteLength)
            })

            const options = {
                format: 'mp3'
            }

            recorderManager.start(options)


        },

        finishRecordingVoice() {
            this.setData({
                isRecordingVoice: false
            })
            // wx.stopRecord() // 结束录音
            const recorderManager = wx.getRecorderManager()
            recorderManager.stop()
        },

        test() {
            console.log(1)
        }
    }
})